﻿using AliIotAmqpWorker.Common;
using AliIotLib;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using SqlSugar;
using System;
using System.IO;
using YL.NetCore.DI;

namespace AliIotAmqpWorker
{
    class Program
    {
        private static DeviceDataTask deviceDataTask;
        private static DeviceStatusTask deviceStatusTask;

        //private static AliAmqp aliAmqp=new AliAmqp();
        static void Main(string[] args)
        {
            ServiceCollection serviceCollection = new ServiceCollection();
            AddParameterSettings(serviceCollection);
            AddDataService(serviceCollection);
            var serviceProvider = serviceCollection.BuildServiceProvider();
            
            deviceDataTask = serviceProvider.GetRequiredService<DeviceDataTask>();
            deviceDataTask.StartTask();

            deviceStatusTask = serviceProvider.GetRequiredService<DeviceStatusTask>();
            deviceStatusTask.StartTask();

            AliAmqp aliAmqp = serviceProvider.GetRequiredService<AliAmqp>();
            aliAmqp.Initial();
            AppDomain.CurrentDomain.ProcessExit += new EventHandler(CurrentDomain_ProcessExit);
        }
        static void CurrentDomain_ProcessExit(object sender, EventArgs e)
        {
            deviceDataTask.Dispose();
            deviceStatusTask.Dispose();
            Console.WriteLine("程序已退出...........................");
        }
        /// <summary>
        /// 配置参数加载
        /// </summary>
        private static void AddParameterSettings(ServiceCollection service)
        {
            var build = new ConfigurationBuilder();
            build.SetBasePath(Directory.GetCurrentDirectory());
            build.AddJsonFile("appsettings.json", true, true);
            var Configuration = build.Build();
            //数据库配置
            string configValue = Configuration.GetSection("DataBaseConfig").GetSection("connString").Value;
            service.AddTransient(serviceProvider =>
            {
                SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
                {
                    ConnectionString = configValue,
                    DbType = DbType.SqlServer,
                    //IsAutoCloseConnection = true,
                    //InitKeyType = InitKeyType.Attribute,
                    //IsShardSameThread = true
                });
                return db;
            });
            
            //mongodb数据库配置
            string connString = Configuration.GetSection("MongoDb").GetSection("connString").Value;
            string dbName = Configuration.GetSection("MongoDb").GetSection("dbName").Value;
            service.AddScoped(serviceProvider =>
            {
                MongoDBHelper mongoDBHelper = new MongoDBHelper(connString, dbName);
                return mongoDBHelper;
            });
            IConfigurationSection section = Configuration.GetSection("AliIot");
            AliIotConfig aliIotConfig = new AliIotConfig();
            aliIotConfig.UserId = section.GetSection("UserId").Value;
            aliIotConfig.RegionId = section.GetSection("RegionId").Value;
            aliIotConfig.ProductKey = section.GetSection("ProductKey").Value;
            aliIotConfig.AccessKeyId = section.GetSection("AccessKeyId").Value;
            aliIotConfig.AccessKeySecret = section.GetSection("AccessKeySecret").Value;
            aliIotConfig.ConsumerGroupId = section.GetSection("ConsumerGroupId").Value;
            aliIotConfig.ClientId = section.GetSection("ClientId").Value;
            aliIotConfig.IotInstanceId = section.GetSection("IotInstanceId").Value;

            service.AddScoped(serviceProvider =>//阿里访问配置
            {
                return aliIotConfig;
            });

            var mongo = service.BuildServiceProvider().GetService<MongoDBHelper>();
            service.AddSingleton(serviceProvider =>
            {
                AliAmqp aliAmqp = new AliAmqp(mongo, aliIotConfig);
                return aliAmqp;
            });
           
            GlobalSetting.ClientCode = aliIotConfig.ClientId;
        }
        /// <summary>
        /// 数据库服务增加
        /// </summary>
        /// <param name="service"></param>
        private static void AddDataService(ServiceCollection service)
        {
            ServiceExtension.RegisterAssembly(service, "Services");
            ServiceExtension.RegisterAssembly(service, "Repository");

            service.AddSingleton<DeviceDataService>();
            service.AddSingleton<DeviceAlarmService>();
            service.AddSingleton<DeviceFailureService>();

            service.AddSingleton<DeviceStatusTask>();
            service.AddSingleton<DeviceDataTask>();
            service.AddScoped<IIotDeviceManager, AliIotDeviceManager>();//阿里访问对象
        }
        
    }
}
